package appClientModule;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * Project Name:myapplet 
 * File Name:CapInstallTest.java 
 * Package Name: 
 * Date:2017��8��22������4:25:48 
 * Copyright (c) 2017, liuhh@hengbao.com All Rights Reserved. 
 * 
*/

/**
 * ClassName:CapInstallTest <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2017��8��22�� ����4:25:48 <br/>
 * 
 * @author liuhh
 * @version
 * @since JDK 1.8
 * @see
 */
public class CapInstallTest {
	
	
	static applet_pcsc ap =null;
	
	public static void test(applet_pcsc ap) {
		CapInstallTest.ap = ap;
		Apdu apdu = new Apdu();
		apdu.setIndex("1");
		apdu.setAPDU("00A40004022FE2");
		String[] sw = new String[] { "6283","9000" };
		apdu.setSW(sw);

		Apdu apdu2 = new Apdu();
		apdu2.setIndex("2");
		apdu2.setAPDU("00B000000A");
		String[] sw2 = new String[] { "9000" };
		apdu2.setSW(sw2);
		
		Apdu apdu3 = new Apdu();
		apdu3.setIndex("3");
		apdu3.setAPDU("00A40004022F00");
		String[] sw3 = new String[] {"9000","6283"};//{ "9000","6283" };
		apdu3.setSW(sw3);
		
		Apdu apdu4 = new Apdu();
		apdu4.setIndex("4");
		apdu4.setAPDU("00B2010430");
		String[] sw4 = new String[] { "9000","6381" };
		apdu4.setSW(sw4);
		
		Apdu apdu5 = new Apdu();
		apdu5.setIndex("5");
		apdu5.setAPDU("00B2020430");
		String[] sw5 = new String[] { "9000","6381" };
		apdu5.setSW(sw5);
		
		List<Apdu> list = new ArrayList<Apdu>();
		list.add(apdu);
		list.add(apdu2);
		list.add(apdu3);
		list.add(apdu4);
		list.add(apdu5);
		convert(list);
		
		Apdu apdu6 = new Apdu();
		apdu6.setIndex("1");
		apdu6.setAPDU("00A40004027F20");
		String[] sw6 = new String[] { "9000" };
		apdu6.setSW(sw6);
		
		Apdu apdu7 = new Apdu();
		apdu7.setIndex("2");
		apdu7.setAPDU("00A40004026F07");
		String[] sw7 = new String[] { "9000" };
		apdu7.setSW(sw7);
		
		Apdu apdu8 = new Apdu();
		apdu8.setIndex("3");
		apdu8.setAPDU("00B0000009");
		String[] sw8 = new String[] { "9000","6283" };
		apdu8.setSW(sw8);

		Apdu apdu9 = new Apdu();
		apdu9.setIndex("4");
		apdu9.setAPDU("00A40004027F25");
		String[] sw9 = new String[] { "9000", "6283" };
		apdu9.setSW(sw9);

		Apdu apdu10 = new Apdu();
		apdu10.setIndex("5");
		apdu10.setAPDU("00B0000009");
		String[] sw10 = new String[] { "9000" };
		apdu10.setSW(sw10);
		
		list.clear();
		list.add(apdu6);
		list.add(apdu7);
		list.add(apdu8);
		list.add(apdu9);
		list.add(apdu10);
		convert(list);
		
		
		Apdu apdu11 = new Apdu();
		apdu11.setIndex("1");
		apdu11.setAPDU("00A40004026F22");
		String[] sw11 = new String[] { "9000","6283" };
		apdu11.setSW(sw11);
		
		Apdu apdu12 = new Apdu();
		apdu12.setIndex("2");
		apdu12.setAPDU("00B000000A");
		String[] sw12 = new String[] { "9000","6283" };
		apdu12.setSW(sw12);
		
		Apdu apdu13 = new Apdu();
		apdu13.setIndex("3");
		apdu13.setAPDU("00A4040410A0000000871002FF86FF0389FFFFFFFF");
		String[] sw13 = new String[] { "9000" };
		apdu13.setSW(sw13);
		
		list.clear();
		list.add(apdu11);
		list.add(apdu12);
		list.add(apdu13);
		convert(list);
		
		
		Apdu apdu14 = new Apdu();
		apdu14.setIndex("14");
		apdu14.setAPDU("00A40004026F07");
		String[] sw14 = new String[] { "9000","6283" };
		apdu14.setSW(sw14);
		
		Apdu apdu15 = new Apdu();
		apdu15.setIndex("15");
		apdu15.setAPDU("00B0000009");
		String[] sw15 = new String[] { "9000","6283" };
		apdu15.setSW(sw15);
		
//		Apdu apdu16 = new Apdu();
//		apdu16.setIndex("16");
//		apdu16.setAPDU("00B0000009");
//		String[] sw16 = new String[] { "9000"};
//		apdu16.setSW(sw16);
		
//		List<Apdu> list = new ArrayList<Apdu>();
		list.clear();
//		list.add(apdu);
//		list.add(apdu2);
//		list.add(apdu3);
//		list.add(apdu4);
//		list.add(apdu5);
//		list.add(apdu6);
//		list.add(apdu7);
//		list.add(apdu8);
//		list.add(apdu9);
//		list.add(apdu10);
//		list.add(apdu11);
//		list.add(apdu12);
//		list.add(apdu13);
		list.add(apdu14);
		list.add(apdu15);
//		list.add(apdu16);
		convert(list);
		
		/*RequestInfo req = new RequestInfo();
		req.setFinishFlag("01");
		req.setCAPDUList(list);

		ObjectMapper mapper = new ObjectMapper();
		String requestInfo = "";
		try {
			requestInfo = mapper.writeValueAsString(req);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} // ������ת����json

		System.out.println(requestInfo);
		@SuppressWarnings("rawtypes")
		Map retMap = new HashMap();
		retMap = install(requestInfo);
		String resp = "";
		try {
			resp = mapper.writeValueAsString(retMap);
		} catch (JsonProcessingException e) {
			System.out.println("CAP����ʧ��");
			e.printStackTrace();
		}
		System.out.println(resp);*/
	}

	public static void convert(List list) {
		RequestInfo req = new RequestInfo();
		req.setFinishFlag("01");
		req.setCAPDUList(list);

		ObjectMapper mapper = new ObjectMapper();
		String requestInfo = "";
		try {
			requestInfo = mapper.writeValueAsString(req);
			System.out.println(requestInfo);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} // ������ת����json
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static Map install(String request) {
		RequestInfo req = null;
		String resp = "";
		Map retMap = new HashMap();
		ObjectMapper mapper = new ObjectMapper();
		try {
			req = mapper.readValue(request, RequestInfo.class);
			if(req!=null){
				List<Apdu> apduList = req.getCAPDUList();
				int apduIndex = 1;
				for(int i=0;i<apduList.size();i++){
					Apdu apdu = apduList.get(i);
					if(Integer.parseInt(apdu.getIndex()) == apduIndex){
						resp = ap.TransmitString(apdu.getAPDU());
						boolean flag = false;
						for(String ru : apdu.getSW()){
							if(resp.substring(resp.length()-4).equals(ru)){
								flag = true;
								break;
							}else {
								continue;
							}
						}
						if(!flag){
							retMap.put("resultCode", 1);
							retMap.put("resultIndex", ""+apduIndex);
							retMap.put("resultResponse", resp.substring(0,resp.length()-4));
							retMap.put("resultSW", resp.substring(resp.length()-4));
							return retMap;
						}
						if(i!=apduList.size()-1){
							apduIndex++;
						}
					}
				}
				retMap.put("resultCode", 0);
				retMap.put("resultIndex", ""+apduIndex);
				retMap.put("resultResponse", resp.substring(0,resp.length()-4));
				retMap.put("resultSW", resp.substring(resp.length()-4));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return retMap;
	}
	public static void main(String[] args) {
		test(null);
	}
}
